<!
//Name: pants.php
//Purpose: This script is for displaying hat images
//CreateDate: 10/02/12 By Steve Vo
// updated: 12/1/12 By Steve Vo
>
<?php

if(isset($_POST['addToCart'])){
	session_name('login');
	session_start();
	if($_SESSION['username'] == null)
	{
		echo '<h1>Please Login to Add to Cart</h1>';
	}
	else
   {

	//DB CONNECT
	$mysql_con = new mysqli("localhost","root","","tc_apparel");
	if(mysqli_connect_errno()){
		echo(",<p>Error creating DB connection</p>");
		exit;
	}
	//Checking if order_id is active otherwise create one		
	if($_SESSION['orderId'] == NULL)
	{
			
		$sql = "INSERT INTO orders(person_id,active_ind)values(".$_SESSION['userId'].",1)";
		$mysql_con->query($sql);
		
		$sql_get = "SELECT order_id FROM orders where person_id = ".$_SESSION['userId']." AND active_ind = 1";
		$result = $mysql_con->query($sql_get);
		$row = $result->fetch_assoc();
		$_SESSION['orderId'] = $row['order_id'];
		session_write_close();
		$sql_insert_cart = "INSERT INTO cart(product_id, order_id)values(".$_POST['product_id'].",".$row['order_id'].")";
		$mysql_con->query($sql_insert_cart);
		$mysql_con->close();
	}	
	else
	{	
		$sql_insert_cart = "INSERT INTO cart(product_id, order_id)values(".$_POST['product_id'].",".$_SESSION['orderId'].")";
		$mysql_con->query($sql_insert_cart);
		$mysql_con->close();
	}
	}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
	<link rel="StyleSheet" type="text/css" href="css/tcaparrel.css">
	<link rel="StyleSheet" href="css/header.css" type="text/css">
	<script type="text/javascript" src="javascript/tcaparrel.js">
	</script> 
</head>

<body class="productPage">
<div class="contentWrapper">
	<div class="header">
		<?php 
		session_name('login');
		session_start();
		if(isset($_SESSION['username']))
		{
			include('loggedInUserHeader.php');
		}
		else
		{
			include ('loginformheader.php');
		}
		?>
	</div>
	
	<div class="menubarContentWrapper">
			<div class="menubar">
				<div class="menuItem" onclick="sweatshirts_click()"><label style="cursor: inherit">SweatShirts</label></div>
				<div class="menuItem" onclick="hats_click()"><label style="cursor: inherit">Hats</label></div>
				<div class="menuItem" onclick="tshirts_click()"><label style="cursor: inherit">T-Shirts</label></div>
				<div class="menuItem" onclick="pants_click()"><label style="cursor: inherit">Pants</label></div>
				
			</div>
	</div>
	
	<h1>Pants</h1>
	
	<table>
		<?php
			$mysql_con = new mysqli("localhost","root","","tc_apparel");
			if(mysqli_connect_errno()){
			echo(",<p>Error creating DB connection</p>");
			exit;
			}
			$sql = "SELECT * FROM products WHERE category = 'PANTS' ORDER BY product_id";
			  $result = $mysql_con->query($sql);
				if (!$result) {
				echo("<p>Error performing query</p>");
				exit();
				}
				
				$mysql_con->close();
			
				
			    
			while($row = $result->fetch_assoc())
			{
				echo '
					<tr>
					<form name="addToCart" action="pants.php" method="post">
						<td>
							<div class="thumbnails">
								<img class="thumbnail" onclick="displayImage(this.src)" src="'.$row['image_loc'].'"></img>
							</div>
							<td>
								<div id="productDetails" class="productDetails">
									<p>'.$row['product_name'].'<br>
										Product id: '.$row['product_id'].'<br>
										Price ONLY $'.$row['price'].	
									'</p>
									<input type ="hidden" name="product_id" value="'.$row['product_id'].'"/>
									<input type="submit" name="addToCart"  value="Add To Cart" />
								</div>
							</td>
						</td>
					</form>
					</tr>';
			}
		?>
		
	</table>
</div>
</body>

</html>
